package com.libui.thirdpartsdk.share

import com.libbase.extension.addUrlParameters
import com.libui.thirdpartsdk.SDKInterface
import com.libui.thirdpartsdk.SDKManager
import com.libui.thirdpartsdk.share.models.*

typealias ShareCompletion = (success: Boolean) -> Unit

interface ShareInterface {
    interface Builder {
        val type: String

        val title: String

        val icon: String?

        val selectedIcon: String?

        fun build(sdk: SDKInterface): ShareInterface
    }

    val shareObjectTypes: List<ShareObjectType>

    val type: String

    val title: String

    val icon: String?

    val selectedIcon: String?

    var isSelected: Boolean

    fun share(shareObject: ShareObjectInterface, completion: ShareCompletion?) {
        when (shareObject) {
            is ShareUrlObject -> {
                addShareParams(shareObject.url) { url ->
                    shareObject.url = url
                    shareUrl(shareObject, completion)
                }
            }
            is ShareFileObject -> {
                shareFile(shareObject, completion)
            }
            is ShareImageObject -> {
                shareImage(shareObject, completion)
            }
            is ShareMiniAppObject -> {
                shareMiniApp(shareObject, completion)
            }
            is ShareCustomizeObject -> {
                shareCustomize(shareObject, completion)
            }
        }
    }

    fun shareUrl(shareObject: ShareUrlObject, completion: ShareCompletion?) {
        completion?.let { it(false) }
    }

    fun shareFile(shareObject: ShareFileObject, completion: ShareCompletion?) {
        completion?.let { it(false) }
    }

    fun shareImage(shareObject: ShareImageObject, completion: ShareCompletion?) {
        completion?.let { it(false) }
    }

    fun shareMiniApp(shareObject: ShareMiniAppObject, completion: ShareCompletion?) {
        completion?.let { it(false) }
    }

    fun shareCustomize(shareObject: ShareCustomizeObject, completion: ShareCompletion?) {
        completion?.let { it(false) }
    }

    fun addShareParams(url: String, completion: (result: String) -> Unit) {
        SDKManager.instance.adapter?.shareParams { shareParams ->
            if (shareParams == null) {
                completion(url)
                return@shareParams
            }

            completion(url.addUrlParameters(shareParams))
        }
    }
}